﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VOL.Core.Utilities;
using VOL.Domain.Infrastructure;
using VOL.Domain.IRepositories;
using VOL.Domain.IServices.MasterData;
using VOL.Entity.DomainModels;
using Microsoft.EntityFrameworkCore;

namespace VOL.Domain.Services.MasterData
{
    public class MaterialProductService : IMaterialProductService
    {
        private readonly IBaseRepository<MaterialProduct> _repository;

        public MaterialProductService(IBaseRepository<MaterialProduct> repository)
        {
            _repository = repository;
        }

        public async Task<WebResponseContent> AddMaterialProduct(MaterialProduct materialProduct)
        {
            var response = new WebResponseContent();
            
            // 检查物料编码是否已存在
            if (await _repository.GetAll(x => x.MaterialCode == materialProduct.MaterialCode).AnyAsync())
            {
                return response.Error("物料编码已存在");
            }

            try
            {
                await _repository.Add(materialProduct);
                return response.OK("添加成功");
            }
            catch (Exception ex)
            {
                return response.Error("添加失败：" + ex.Message);
            }
        }

        public async Task<WebResponseContent> DeleteMaterialProduct(int id)
        {
            var response = new WebResponseContent();
            
            var materialProduct = await _repository.GetAll(x => x.Id == id).FirstOrDefaultAsync();
            if (materialProduct == null)
            {
                return response.Error("物料产品不存在");
            }

            try
            {
                await _repository.Delete(materialProduct);
                return response.OK("删除成功");
            }
            catch (Exception ex)
            {
                return response.Error("删除失败：" + ex.Message);
            }
        }

        public async Task<WebResponseContent> GetMaterialProduct(string materialCode, string materialName, int pageIndex, int pageSize)
        {
            var response = new WebResponseContent();
            try
            {
                var query = _repository.GetAll();
                if (!string.IsNullOrEmpty(materialCode))
                {
                    query = query.Where(x => x.MaterialCode.Contains(materialCode));
                }
                if (!string.IsNullOrEmpty(materialName))
                {
                    query = query.Where(x => x.MaterialName.Contains(materialName));
                }
                var totalCount = await query.CountAsync();
                var products = await query.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();
                return response.OK("查询成功", new { TotalCount = totalCount, Products = products });
            }
            catch (Exception ex)
            {
                return response.Error("查询失败：" + ex.Message);
            }
        }

        public async Task<WebResponseContent> UpdateMaterialProduct(MaterialProduct materialProduct)
        {
            var response = new WebResponseContent();
            
            var existingProduct = await _repository.GetAll(x => x.Id == materialProduct.Id).FirstOrDefaultAsync();
            if (existingProduct == null)
            {
                return response.Error("物料产品不存在");
            }

            // 检查物料编码是否与其他记录重复
            if (await _repository.GetAll(x => x.MaterialCode == materialProduct.MaterialCode && x.Id != materialProduct.Id).AnyAsync())
            {
                return response.Error("物料编码已存在");
            }

            try
            {
                // 更新实体属性
                existingProduct.MaterialCode = materialProduct.MaterialCode;
                existingProduct.MaterialName = materialProduct.MaterialName;
                existingProduct.Specification = materialProduct.Specification;
                existingProduct.Flats = materialProduct.Flats;
                existingProduct.FlatsMaterial = materialProduct.FlatsMaterial;
                existingProduct.IsHighValue = materialProduct.IsHighValue;
                existingProduct.IsLock = materialProduct.IsLock;
                existingProduct.IsBatch = materialProduct.IsBatch;
                existingProduct.SafetyStock = materialProduct.SafetyStock;
                existingProduct.Descs = materialProduct.Descs;

                await _repository.Update(existingProduct);
                return response.OK("更新成功");
            }
            catch (Exception ex)
            {
                return response.Error("更新失败：" + ex.Message);
            }
        }
    }
}
